package com.crm.system.controller.returnMoney;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.returnMoney.BankFlowExamine;
import com.crm.model.qo.returnMoney.BankFlowExamineQO;
import com.crm.model.qo.returnMoney.ReturnMoneyQO;
import com.crm.model.vo.customerManager.CustomerPayAllInfoVO;
import com.crm.model.vo.finishOrderProduct.FinishOrderProductVO;
import com.crm.model.vo.returnMoney.BankFlowExamineVO;
import com.crm.model.vo.returnMoney.ReturnMoneyVO;
import com.crm.service.customerManager.CustomerPayDbService;
import com.crm.service.returnMoney.BankFlowExamineDbService;
import com.crm.system.service.returnMoney.BankFlowExamineService;
import com.crm.system.service.returnMoney.ReturnMoneyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 *
 * @Author xielinqi
 * @Date 10:37 上午 2021/11/2
 */
@RestController
@RequestMapping("/bankFlowExamine")
@Slf4j
public class BankFlowExamineController {

    @Autowired
    private BankFlowExamineService bankFlowExamineService;
    @Autowired
    private BankFlowExamineDbService bankFlowExamineDbService;
    @Autowired
    private ReturnMoneyService returnMoneyService;
    @Autowired
    private CustomerPayDbService customerPayDbService;

    /**
     *流水审核列表
     *
     * @Param request
     * @Param pageIndex
     * @Param pageSize
     * @Param bankFlowExamineQO
     * @Author xielinqi
     * @Date 4:07 下午 2021/11/2
     */
    @PostMapping("/queryList")
    public DataResponse queryList(HttpServletRequest request,
                                                   @RequestParam(value = "pageIndex", required = false) Integer pageIndex,//页码
                                                   @RequestParam(value = "pageSize", required = false) Integer pageSize,//分页大小
                                                   BankFlowExamineQO bankFlowExamineQO
    ){
        try{
            DhPageInfo<BankFlowExamineVO> list = bankFlowExamineService.selectPageList(pageIndex, pageSize, bankFlowExamineQO);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            log.error("order customerOrderBackMoneyList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 提交流水审核
     * @Param bankFlowExamine
     * @Author xielinqi
     * @Date 9:34 上午 2021/11/3
     */
    @PostMapping("/examine")
    public DataResponse examine(BankFlowExamine bankFlowExamine){
        return bankFlowExamineService.examine(bankFlowExamine);
    }

    /**
     * 查询审核信息（包含订单详情、合同信息、产品信息、解除关联备注等）
     * @Param examineId
     * @Author xielinqi
     * @Date 2:00 下午 2021/11/3
     */
    @PostMapping("/queryProductOderInfo")
    public DataResponse queryProductOderInfo(String examineId){
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), bankFlowExamineService.getDetails(examineId));
    }

    /**
     * 产品列表
     * @Param examineId
     * @Param pageIndex
     * @Param pageSize
     * @Author xielinqi
     * @Date 9:37 上午 2021/11/8
     */
    @PostMapping("/queryProductList")
    public DataResponse queryProductList(BankFlowExamineQO bankFlowExamineQO,
         @RequestParam(value = "pageIndex", required = false) Integer pageIndex,//页码
         @RequestParam(value = "pageSize", required = false) Integer pageSize//分页大小
    ){
        DhPageInfo<FinishOrderProductVO> list = bankFlowExamineService.selectProductPageList(pageIndex, pageSize, bankFlowExamineQO);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    /**
     * 回款列表
     * @Param bankFlowExamineQO
     * @Param pageIndex
     * @Param pageSize
     * @Author xielinqi
     * @Date 10:35 上午 2021/11/8
     */
    @PostMapping("/queryReturnMoneyList")
    public DataResponse queryReturnMoneyList(@RequestParam(value = "pageIndex", required = false) Integer pageIndex,//页码
                                             @RequestParam(value = "pageSize", required = false) Integer pageSize,//分页大小
                                             String examineId
    ){
        BankFlowExamine examine = bankFlowExamineDbService.selectByExamineId(examineId);
        if(examine == null){
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }
        ReturnMoneyQO returnMoneyQO = new ReturnMoneyQO();
        returnMoneyQO.setOrderCode(examine.getProductOrderId());
        DhPageInfo<ReturnMoneyVO> list = returnMoneyService.selectCustomerProductList(returnMoneyQO,pageIndex,pageSize,"createTm","asc");
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    /**
     * 当前审核
     * @Param examineId
     * @Author xielinqi
     * @Date 6:02 下午 2021/11/8
     */
    @PostMapping("/queryExamineInfo")
    public DataResponse queryExamineInfo(String examineId){
        ReturnMoneyVO vo = bankFlowExamineDbService.selectCurExamineReturnMoney(examineId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),vo);
    }

    /**
     * 根据订单id解除关联
     * @Param bankFlowExamine
     * @Author xielinqi
     * @Date 4:29 下午 2021/11/9
     */
    @PostMapping("/relieveRelationByOrderId")
    public DataResponse relieveRelationByProductOrderId(BankFlowExamine bankFlowExamine){
        return bankFlowExamineService.relieveRelationByProductOrderId(bankFlowExamine);
    }

    /**
     * 查询
     * @Param examineId
     * @Author xielinqi
     * @Date 3:20 下午 2021/11/11
     */
    @PostMapping("/queryExaminePayInfo")
    public DataResponse queryExaminePayInfo(String examineId){
        BankFlowExamine examine = bankFlowExamineDbService.selectByExamineId(examineId);
        if(examine == null){
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }
        HashMap data = new HashMap();
        if(StringUtils.isNoneBlank(examine.getCustomerPayId())){
            //充值记录
            CustomerPayAllInfoVO customerPayAllInfoVO = customerPayDbService.queryDetail(examine.getCustomerPayId());
            data.put("customerPay", customerPayAllInfoVO);
        }
        data.put("examine", examine);

        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),data);
    }

}
